
package com.kehutong.house.service;
import com.kehutong.house.entity.Community;
        import com.kehutong.house.entity.Parking;
        import com.kehutong.house.entity.ParkingClassify;
        import org.coraframework.inject.Inject;
        import org.coraframework.json.JSONObject;
        import org.coraframework.mvc.http.Bean;
        import org.coraframework.mvc.http.ReqMapping;
        import org.coraframework.mvc.http.result.Result;
        import org.coraframework.mvc.webservice.WebService;
        import org.coraframework.orm.Pool;
        import org.coraframework.orm.jdbc.JdbcSession;
        import org.coraframework.orm.jdbc.Page;
        import org.coraframework.orm.jdbc.execute.entity.FindOne;
        import org.coraframework.orm.jdbc.execute.entity.FindPage;
        import org.coraframework.util.Objects;

        import java.util.List;

@WebService("/basic/parking/classify/service/api")
public class ParkingClassifyServiceApi {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private CommunityService communityService;

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        FindPage<ParkingClassify> find = jdbcSession.findPage(ParkingClassify.class)
                .eq(jsonObject, "name")
                .eq(jsonObject,"communityName").and()
                .eq("deleted", false);
        Page<ParkingClassify> page = find.page(jsonObject).exe();

        return page;
    }

    @ReqMapping("/get")
    public Object get(ParkingClassify item) {
        return item;
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(ParkingClassify parkingClassify) {
        FindOne<ParkingClassify> find = jdbcSession.findOne(ParkingClassify.class)
                .eq("deleted", false)
                .eq("communityNo", parkingClassify.getCommunityNo())
                .eq("name", parkingClassify.getName());
        ParkingClassify item = find.exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "当前小区下已有相同的车位分类信息，请修改后重试");
        }

        Community com = Pool.get(Community.class, parkingClassify.getCommunityNo());
        parkingClassify.setDepartmentNo(com.getParent_id());
        parkingClassify.setDepartmentNos(com.getParent_ids());

        jdbcSession.insert(parkingClassify);
        return Result.success(parkingClassify);
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(ParkingClassify parkingClassify) throws Exception {
        FindOne<ParkingClassify> find = jdbcSession.findOne(ParkingClassify.class)
                .eq("deleted", false)
                .eq("communityNo", parkingClassify.getCommunityNo())
                .eq("name", parkingClassify.getName())
                .noEq("id", parkingClassify.getId());
        ParkingClassify item = find.exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "当前小区下已有相同的车位分类信息，请修改后重试");
        }

        Community com = Pool.get(Community.class, parkingClassify.getCommunityNo());
        parkingClassify.setDepartmentNo(com.getParent_id());
        parkingClassify.setDepartmentNos(com.getParent_ids());

        jdbcSession.updateById(parkingClassify);
        return Result.success(parkingClassify);
    }

    @ReqMapping("/delete")
    public Object delete(ParkingClassify item) {
        List<Parking> parkings = jdbcSession.findArray(Parking.class)
                .eq("communityNo", item.getCommunityNo())
                .eq("parkingClassifyNo", item.getId())
                .eq("deleted", false)
                .exe();

        if(Objects.nonEmpty(parkings)){
            return Result.error(0, "需先删除该车位分类下的车位，再删除该车位分类！");
        }


        item.setDeleted(true);
        jdbcSession.updateById(item);

        return Result.success();
    }

}
